﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;
using Ideas2Realize.CommonInterfaces;
using System.Security.Principal;
using System.Web.Routing;
using Ideas2Realize.Data.Business.Interface;
using System.Web.Security;
using Ideas2Realize.Helpers;
using System.Threading;
using Ideas2Realize.Data;

namespace Ideas2Realize.Controllers
{
    public class HomeController : BaseController
    {

        public ActionResult Script()
        {
            SetConfigs();

            Assembly assembly = Assembly.GetExecutingAssembly();
            Version ver = assembly.GetName().Version;


            if (ver.Build > 0 || ver.Revision > 0)
                ViewBag.Version = string.Format("v{0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision);
            else
                ViewBag.Version = string.Format("v{0}.{1}", ver.Major, ver.Minor);

            return View();
        }
        
        [Authorize()]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize()]
        public ActionResult Dashboard()
        {
            return View();
        }

        [Authorize()]
        public ActionResult News()
        {
            return View();
        }

        [Authorize()]
        public ActionResult Help()
        {
            return Redirect("http://www.google.se/");
        }

        public void Logout()
        {
            FormsAuthentication.SignOut();

            string url = UrlHelper.GenerateUrl(null, "Index", "Home", RouteData.Values, RouteTable.Routes, ControllerContext.RequestContext, false);
           
            Response.Redirect(url);
        }

        [HttpPost()]
        public JsonResult Login(FormCollection collection)
        {
            try
            {
                bool remember = collection["rememberme"] == "true" ? true : false; 
                bool auth = AuthorizationService.Login(collection["Username"], collection["Password"]);

                string roles = "";

                foreach (var r in UserRepository.GetRoles(collection["Username"])) {
                    roles += r + ",";
                }

                TimeSpan timeout = FormsAuthentication.Timeout; 

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                    2,
                    collection["Username"], 
                    DateTime.Now,
                    DateTime.Now.AddMinutes(timeout.TotalMinutes),
                    remember,
                    roles.TrimEnd(',')
                    );
                
                string encrypted = FormsAuthentication.Encrypt(ticket);

                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName);
                cookie.Value = encrypted;
                cookie.Expires = ticket.Expiration;

                HttpContext.Response.SetCookie(cookie);

                string url = UrlHelper.GenerateUrl(null, "Index", "Home", RouteData.Values, RouteTable.Routes, ControllerContext.RequestContext, false);

                return Success(new { url = url});
            }
            catch (Exception ex)
            {
                return Error(ex);
            }
        }

        public ActionResult Login()
        {
            return View();
        }

    }
}
